package cn.laike.modules.dev.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;

import cn.laike.common.utils.PageUtils;
import cn.laike.common.utils.Pinyin;
import cn.laike.common.utils.R;
import cn.laike.common.validator.ValidatorUtils;
import cn.laike.modules.dev.entity.CityEntity;
import cn.laike.modules.dev.service.CityService;



/**
 * 城市列表
 *
 * @author ziye
 * @email 524748907@qq.com
 * @date 2019-02-18 11:54:52
 */
@RestController
@RequestMapping("dev/city")
public class CityController {
    @Autowired
    private CityService cityService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("dev:city:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = cityService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("dev:city:info")
    public R info(@PathVariable("id") Long id){
        CityEntity city = cityService.selectById(id);

        return R.ok().put("city", city);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("dev:city:save")
    public R save(@RequestBody CityEntity city){
    	city.setHeadChar(Pinyin.getPinYinFirstHeadChar(city.getName()));
    	city.setDelFlag("0");
    	city.setCreateTime(new Date());
        cityService.insert(city);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("dev:city:update")
    public R update(@RequestBody CityEntity city){
        ValidatorUtils.validateEntity(city);
        city.setHeadChar(Pinyin.getPinYinFirstHeadChar(city.getName()));
        cityService.updateAllColumnById(city);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("dev:city:delete")
    public R delete(@RequestBody Integer[] ids){
    	List<CityEntity> list = Lists.newArrayList();
    	for (Integer id : ids) {
    		CityEntity city = new CityEntity();
    		city.setId(id);
    		city.setDelFlag("1");
    		list.add(city);
		}
    	cityService.updateBatchById(list);

        return R.ok();
    }
    
    /**
     * 获取所有城市列表
     * @return
     */
    @RequestMapping("/querylist")
    public R querylist(){
        List<CityEntity> citylist = cityService.queryList();
        return R.ok().put("citylist", citylist);
    }

}
